package com.taotao.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.BaseResult;
import com.taotao.mapper.EyChinaMapper;
import com.taotao.pojo.EyChina;
import com.taotao.pojo.EyChinaExample;
import com.taotao.pojo.EyChinaExample.Criteria;
import com.taotao.service.ChinaService;
import com.taotao.service.ParamService;

@Service
public class ChinaServiceImpl implements ChinaService {

	@Autowired
	EyChinaMapper eyChinaMapper;
	@Autowired
	ParamService ParamService;
	
	BaseResult baseResult = new BaseResult();
	
	@Override
	public Map<String, Object> getChinaList(Map<String, String> map) {
		try {
			String parentId = map.get("parentId");
			String grade = map.get("grade");
			String language = ParamService.getParam("language");
			
			EyChinaExample example = new EyChinaExample();
	  		Criteria criteria = example.createCriteria();
	  		if(StringUtils.isNotBlank(parentId)){
	  			criteria.andParentIdEqualTo(Integer.parseInt(parentId));
	  			example.setOrderByClause("  CAST(show_order AS SIGNED) asc,name_en asc ");
	  		}
	  		if(StringUtils.isNotBlank(grade)){
	  			criteria.andGradeEqualTo(grade);
	  			example.setOrderByClause(" CAST(show_order AS SIGNED) asc, CAST(id AS SIGNED) asc ");
	  		}
	  		List<EyChina> list = eyChinaMapper.selectByExample(example);
	  		if("cn".equalsIgnoreCase(language)){
	  			for(EyChina c:list){
	  				c.setNameEn(c.getName());
	  			}
	  		}
	  		
			return baseResult.getSuccMap(list);
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorJsonObj("网络繁忙，请稍后再试");
		}
	}

}
